package com.weiyi.mashangzhu.Utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeFormat {

    public static final String FCN_YM = "yyyy年MM月";
    public static final String FCN_DATA = "yyyy年MM月dd日";
    public static final String FCN_FULL = "yyyy年MM月dd日 HH时mm分ss秒";
    public static final String FEN_DATA = "yyyy-MM-dd";
    public static final String FEN_FULL = "yyyy-MM-dd HH:mm:ss";

    private static final long ONE_DAY_MS = 24 * 60 * 60 * 1000;

    /**
     * 获取当前系统时间
     */
    public static String getSystemDate(String formatType) {
        return dateToString(new Date(), formatType);
    }

    /**
     * 获取当前系统时间毫秒值
     */
    public static long getSystemMilliSecond() {
        return Calendar.getInstance().getTimeInMillis();
    }

    /**
     * Date —to—> String
     */
    public static String dateToString(Date data, String formatType) {
        String formatTime = "0";
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        try {
            formatTime = sdf.format(data);
        } catch (Exception e) {

        }
        return formatTime;
    }

    /**
     * Date —to—> Long
     */
    public static long dateToLong(Date date) {
        return date.getTime();
    }

    /**
     * String —to—> Date
     */
    public static Date stringToDate(String stringTime, String formatType) {
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        Date date = new Date();
        try {
            date = sdf.parse(stringTime);
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * String —to—> Long
     */
    public static long stringToLong(String stringTime, String formatType) {
        Date date = stringToDate(stringTime, formatType);
        return date == null ? 0l : dateToLong(date);
    }

    /**
     * Long —to—> Date
     */
    public static Date longToDate(long currentTime, String formatType) {
        Date currentDate = new Date(currentTime);
        String stringTime = dateToString(currentDate, formatType);
        Date date = stringToDate(stringTime, formatType);
        return date;
    }

    /**
     * Long —to—> String
     */
    public static String longToString(long currentTime, String formatType) {
        Date date = longToDate(currentTime, formatType);
        String stringTime = dateToString(date, formatType);
        return stringTime;
    }

    /**
     * Date —to—> Calendar
     */
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    /**
     * String —to—> Calendar
     */
    public static Calendar stringToCalendar(String strTime, String formatType) {
        Date date = stringToDate(strTime, formatType);
        return dataToCalendar(date);
    }

    /**
     * Calendar —to—> Date
     */
    public static Date calendarToDate(Calendar calendar) {
        return calendar.getTime();
    }

    /**
     * Calendar —to—> String
     */
    public static String calendarToString(Calendar calendar, String formatType) {
        Date date = calendarToDate(calendar);
        return dateToString(date, formatType);
    }

    /**
     * String —to—> Timestamp
     */
    public static Timestamp stringToTimestamp(String strTime) {
        Timestamp timestamp = null;
        try {
            timestamp = Timestamp.valueOf(strTime);
        } catch (Exception e) {

        }
        return timestamp;
    }

    /**
     * Date —to—> TimeStamp
     */
    public static Timestamp dataToTimestamp(Date date, String formatType) {
        String strTime = dateToString(date, formatType);
        return stringToTimestamp(strTime);
    }

//    long l = System.currentTimeMillis();
//    Timestamp ts = new Timestamp(l);

    /**
     * Timestamp —to—> String
     */
    public static String TimestampToString(Timestamp timestamp, String formatType) {
        String formatTime = "0";
        SimpleDateFormat sdf = new SimpleDateFormat(formatType);
        try {
            formatTime = sdf.format(timestamp);
        } catch (Exception e) {

        }
        return formatTime;
//        return timestamp.toString();
    }

    /**
     * Timestamp —to—> Date
     */
    public static Date TimestampToDate(Timestamp timestamp) {
        Date date = new Date();
        try {
            date = timestamp;
        } catch (Exception e) {

        }
        return date;
    }

    /**
     * 计算两个日期之间的天数
     */
    public static int betweenDays(String startTime, String endTime) {

        Date startDate = stringToDate(startTime, TimeFormat.FEN_DATA);
        Date endDate = stringToDate(endTime, TimeFormat.FEN_DATA);

        // 计算日期从开始时间于结束时间的0时计算
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(startDate);
        startCalendar.set(Calendar.HOUR_OF_DAY, 0);
        startCalendar.set(Calendar.MINUTE, 0);
        startCalendar.set(Calendar.SECOND, 0);
        startCalendar.set(Calendar.MILLISECOND, 0);
        String s = calendarToString(startCalendar, FEN_FULL);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(endDate);
        endCalendar.set(Calendar.HOUR_OF_DAY, 0);
        endCalendar.set(Calendar.MINUTE, 0);
        endCalendar.set(Calendar.SECOND, 0);
        endCalendar.set(Calendar.MILLISECOND, 0);
        String s1 = calendarToString(endCalendar, FEN_FULL);

        return (int) ((endCalendar.getTimeInMillis() - startCalendar.getTimeInMillis()) / (ONE_DAY_MS));

    }
}
